package graduation.claf.controller;

import graduation.claf.dao.OwnershipMapper;
import graduation.claf.pojo.Ownership;
import graduation.claf.tools.ResponseCode;
import graduation.claf.utils.ServerResponse;
import graduation.claf.vo.MemberInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/portal")
public class OwnershipController {

    @Autowired
    OwnershipMapper ownershipMapper;

    @PostMapping("/ownership/members.do")
    public ServerResponse queryMembers(String bookId){
        List<Ownership> ownershipList = ownershipMapper.selectMember(bookId);
        List<MemberInfo> members = new ArrayList<>();
        for (int i=0;i<ownershipList.size();i++){
            Ownership temp = ownershipList.get(i);
            String id = temp.getUserid();
            String type = temp.getRelationship();
            MemberInfo memberInfo = new MemberInfo(id,type);
            members.add(memberInfo);
        }
        if (members.size()== 0)
            return ServerResponse.createServerResponseByFail(
                    ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMsg());
        return ServerResponse.createServerResponseBySuccess(members);
    }
}
